package com.zhentao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.common.service.FileStorageService;
import com.zhentao.common.service.impl.FileStorageServiceImpl;
import com.zhentao.mapper.BuProductMapper;
import com.zhentao.model.common.buiness.dto.BuProductUpAndDownDto;
import com.zhentao.model.common.buiness.pojo.Product;
import com.zhentao.model.common.buiness.vo.BuProductVO;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.BuProductService;
import lombok.SneakyThrows;
import okio.Utf8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BuProductServiceImpl implements BuProductService {
    @Autowired
    private BuProductMapper buProductMapper;
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 商品添加共呢个
     * @param product
     * @return
     */
    @Override
    public ResponseResult productSave(Product product) {
        if (product==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        product.setAudit(0);
        product.setStatus(0);
        product.setCreatetime(new Date());
        buProductMapper.insert(product);
        return ResponseResult.okResult("添加成功");
    }

    /**
     * 商家端：商品查询
     * @param vo
     * @return
     */
    @Override
    public ResponseResult productFindAll(BuProductVO vo) {
        if (vo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        PageHelper.startPage(vo.getPage(), vo.getSize());
        List<BuProductVO>productFindAll=buProductMapper.productFindAll(vo);
        PageInfo<BuProductVO>pageInfo=new PageInfo<>(productFindAll);
        return ResponseResult.okResult(pageInfo);
    }


    /**
     * 商家端：处理商家端的图片
     * @param file
     * @return
     */
    @SneakyThrows
    @Override
    public String fiie(MultipartFile file) {
        String filename= UUID.randomUUID()+file.getOriginalFilename();
        String path = fileStorageService.uploadImage(filename, file.getInputStream());
        return path;
    }


    /**
     * 商家端：根据商品id查询商品
     * @param id
     * @return
     */
    @Override
    public ResponseResult findByProductId(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Product product = buProductMapper.selectById(id);
        return ResponseResult.okResult(product);
    }


    /**
     * 商家端：商品的上下架
     * @param dto
     * @return
     */
    @Override
    public ResponseResult upAndDown(BuProductUpAndDownDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Product product = buProductMapper.selectById(dto.getId());
        if (product==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        product.setStatus(dto.getStatus());
        buProductMapper.updateById(product);
        return ResponseResult.okResult("修改成功");
    }


    /**
     * 商家端：用户信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult userInfo(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Product product = buProductMapper.selectById(id);
        return ResponseResult.okResult(product);
    }
}
